home *** CD-ROM | disk | FTP | other *** search
- unit Soundfrm;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls, Grids, DBGrids, DB, DBTables,
- Memtable, Scanners, Soundex;
-
- type
- TEmployeeSearchForm = class(TForm)
- EmployeeDataSource: TDataSource;
- MatchingDataSource: TDataSource;
- EmployeeTable: TTable;
- EmployeeDBGrid: TDBGrid;
- MatchingDBGrid: TDBGrid;
- SearchEdit: TEdit;
- SoundexSearchButton: TButton;
- procedure SoundexSearchButtonClick(Sender: TObject);
- private
- { Private declarations }
- MatchingInMemoryTable: TInMemoryTable;
- public
- { Public declarations }
- end;
-
- var
- EmployeeSearchForm: TEmployeeSearchForm;
-
- implementation
-
- {$R *.DFM}
-
- { Table scanner's tranfer condition function. }
- function SoundexMatch( SearchTable: TTable ): Boolean; far;
- begin
- { Compare soundex code of current record's LastName field
- with the soundex code of the user provided text.
- Notice that you have to reference the EmployeeSearchForm
- explicitly, since this is not a method of that form, but
- a free, utility function. }
- if SoundexCode( SearchTable.FieldByName('LastName').AsString )
- = SoundexCode( EmployeeSearchForm.SearchEdit.Text )
- then Result := True
- else Result := False;
- end;
-
- { Table scanner's transfer action procedure. }
- procedure TransferMatchingRecord(
- SourceTable, DestinationTable: TTable
- ); far;
- var
- i: Integer;
- begin
- { Assume SourceTable and DestinationTable have the
- same structure. Make a field-by-field copy. }
- DestinationTable.Append;
- for i := 0 to SourceTable.FieldCount - 1 do
- begin
- DestinationTable.Fields[ i ].AsString :=
- SourceTable.Fields[ i ].AsString;
- end;
- end;
-
- procedure TEmployeeSearchForm.SoundexSearchButtonClick(
- Sender: TObject);
- var
- MatchingScanner: TTableConditionalTransferScanner;
- begin
- MatchingDataSource.DataSet := Nil;
- { If nothing to search, don't bother further. }
- if SearchEdit.Text = '' then exit;
- { If the temporary in-memory table is alive, kill it. }
- if MatchingInMemoryTable <> Nil then
- begin
- MatchingInMemoryTable.Close;
- MatchingInMemoryTable.Free;
- MatchingInMemoryTable := Nil;
- end;
- { Make sure the Employee table is open. }
- EmployeeTable.open;
- { Create new temporary in-memory table. The table will be
- empty, ready to accept records that match with
- the user provided search criteria. }
- MatchingInMemoryTable := TInMemoryTable.CreateLike(
- EmployeeTable,
- 'Mem',
- Self
- );
- { Link in-memory table to grid via MatchingDataSource. }
- MatchingInMemoryTable.Open;
- MatchingDataSource.DataSet := MatchingInMemoryTable;
- { Create an appropriate table scanner. }
- MatchingScanner := TTableConditionalTransferScanner.Create(
- EmployeeTable, { Source Table }
- MatchingInMemoryTable, { Destination Table }
- TransferMatchingRecord, { Transfer Action }
- SoundexMatch { Transfer Condition }
- );
- { Scan through empolyee table, transferring sound alikes
- to temporary in-memory table. }
- MatchingScanner.Execute;
- MatchingScanner.Free;
- end;
-
- end.
-